package de.uni_saarland.coli.doclists.suffixtree.disc;

import de.uni_saarland.coli.doclists.index.filesystem.CachedObjectDiscList;
import de.uni_saarland.coli.doclists.suffixtree.INodeManager;
import de.uni_saarland.coli.doclists.suffixtree.STreeNode;

/**
 * 
 * @author stefan
 * 
 *         manages suffix tree nodes using a cached DiscList
 */
public class DiscNodeManager implements INodeManager {
	/**
	 * the nodes
	 */
	CachedObjectDiscList<STreeNode> nodeList;
	
	/**
	 * Constructor.
	 * @param size size of the cache
	 */
	public DiscNodeManager(int size) {
		this.nodeList = new CachedObjectDiscList<STreeNode>(size) {
			
			@Override
			public STreeNode createObject(byte[] bytes) {
				return new STreeNode(bytes);
			}
		};
	}
	
	@Override
	public void save(String path, String baseName) {
		this.nodeList.save(path, baseName);
	}

	@Override
	public void open(String path, String baseName) {
		this.nodeList.open(path, baseName);
	}

	@Override
	public STreeNode createNode() {
		STreeNode newNode = new STreeNode();
		this.nodeList.addObject(newNode);
		return newNode;
	}

	@Override
	public STreeNode getNode(int id) {
		return this.nodeList.getObject(id);
	}

	@Override
	public int getNextNodeID() {
		return (int)this.nodeList.indexSize();
	}

}
